import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-02-20
 * Time: 10:16
 */
public class TestDemo {
    public static String myToString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret+=array[i];
            if(i<array.length-1){
            ret+=",";
            }
        }
        ret+="]";
        return ret;
    }
    public static int[] copyOf(int[] array){
        int[]ret=new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i]=array[i];
        }
        return ret;
    }
    public static int binarySearch(int[]array,int num){
        int left=0;int right= array.length-1;
        while(left<=right){
            int mid=(left+right)/2;int tmp=0;
            if(num>array[mid]){
               left=mid+1;
            }
            else if(num<array[mid]){
               right=mid-1;
            }
            else{
                return mid;
            }
        }
        return -1;
    }
    public static boolean isSort(int[]arr){
        for (int i = 0; i < arr.length; i++) {
            if (i< arr.length-1&&arr[i] >= arr[i+1]) {
                return false;
            }
        }
        return true;
    }
    public static void bubbleSort(int[]arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < arr.length-1-i; j++) {
                int tmp=0;
                if(arr[j]>arr[j+1]){
                    flg=true;
                    tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                }
            }
            if(flg==false){
                break;
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {1,4,7,2,5,8,9,10,11};
        System.out.println("before: "+Arrays.toString(array));
        bubbleSort(array);
        System.out.println("after:  "+Arrays.toString(array));
    }
}
